package ejb.manager;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import ejb.dao.CityDAO;
import ejb.entity.City;

@Stateless
public class DefaultCityManager implements CityManager {
	@EJB
	CityDAO dao;

	private double d(int x, int y, City c) {
        int dx = x - c.getPositionX();
        int dy = y - c.getPositionY();
        return Math.sqrt(dx*dx+dy*dy);
    }

    @Override
	public double approxPress(int x, int y) {
                  List<City> cities = dao.getCities();
        double maxd = 0;
        for(City c : cities)
            if(maxd < d(x, y, c))
                maxd = d(x, y, c);
        
        double sumad = 0, sumap = 0;
        for(City c: cities) {
            double dd = (maxd - d(x, y, c));
            sumad += (dd*dd*dd*dd);
            sumap += (dd*dd*dd*dd) * c.getPressure();
        }
        return sumap/sumad;
    }

    @Override
	public double approxTemp(int x, int y) {
              List<City> cities = dao.getCities();
        double maxd = 0;
        for(City c : cities)
            if(maxd < d(x, y, c))
                maxd = d(x, y, c);
        
        double sumad = 0, sumat = 0;
        for(City c: cities) {
            double dd = (maxd - d(x, y, c));
            sumad += (dd*dd*dd*dd);
            sumat += (dd*dd*dd*dd) * c.getTemperature();
        }
        return sumat/sumad;
    }
    
    @Override
    public int approxConds(int x, int y) {
    	List<City> cities = dao.getCities();
    	double mind = Double.MAX_VALUE;
    	int conds = 0;
    	for(City c : cities) {
    		if(mind > d(x, y, c)) {
    			mind = d(x, y, c);
    			conds = c.getConditions();
    		}
    	}
    	return conds;
    }
    
    @Override
	public double approxPress(int x, int y, List<City> cities) throws NullPointerException {

        double maxd = 0;
        for(City c : cities)
            if(maxd < d(x, y, c))
                maxd = d(x, y, c);
        
        double sumad = 0, sumap = 0;
        for(City c: cities) {
            double dd = (maxd - d(x, y, c));
            sumad += (dd*dd*dd*dd);
            sumap += (dd*dd*dd*dd) * c.getPressure();
        }
        return sumap/sumad;
    }

    @Override
	public double approxTemp(int x, int y, List<City> cities) throws NullPointerException {

        double maxd = 0;
        for(City c : cities)
            if(maxd < d(x, y, c))
                maxd = d(x, y, c);
        
        double sumad = 0, sumat = 0;
        for(City c: cities) {
            double dd = (maxd - d(x, y, c));
            sumad += (dd*dd*dd*dd);
            sumat += (dd*dd*dd*dd) * c.getTemperature();
        }
        return sumat/sumad;
    }
    
    @Override
    public int approxConds(int x, int y, List<City> cities) throws NullPointerException {

    	double mind = Double.MAX_VALUE;
    	int conds = 0;
    	for(City c : cities) {
    		if(mind > d(x, y, c)) {
    			mind = d(x, y, c);
    			conds = c.getConditions();
    		}
    	}
    	return conds;
    }
    
    @Override
    public List<City> getAllCities(){
    	List<City> cities = dao.getCities();
    	return cities;
    }

}
